﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace TEST
{
	public class Congfig
	{
		public static string GetAppSetting(string key)
		{
			return System.Configuration.ConfigurationManager.AppSettings[key].ToString();
		}
	}

	public static class JsonSerlize
	{
		public static string Serializer(this object s)
		{
			JavaScriptSerializer serializer = new JavaScriptSerializer();
			return serializer.Serialize(s);
		}

		public static T DeSerialize<T>(this string jsonStr)
		{
			JavaScriptSerializer serializer = new JavaScriptSerializer();
			return serializer.Deserialize<T>(jsonStr);
		}
		static void Test()
		{
			Order ss = new Order
			{
				orderList = new List<OrderDetail>() 
				{
					new OrderDetail
					{
						orderId="hh123",
						status=1,
						creationTime ="2013-12-23 12:00:00",
						overTime="2013-12-23 12:30:00",
						payTime="2013-12-24 12:30:00",
						expireTime="2013-12-25 12:30:00",
						rejectTime="2013-12-25 12:22:22",
						cancelTime="1900-01-01 00:00:00",
						departure="HKP",
						arrival="HK",
						arrivalTime="2014-01-01 00:00:00",
						departureDate="2013-12-30",
						departureTime="2013-12-30 12:00:00",
						flightNo="F925",
						mailNo="hhh123",
						mailType=1,
						mailStatus=1,
						realPay=190.00f,
						shouldPay=199.00f
					},
				new OrderDetail{},
				new OrderDetail{},
				new OrderDetail{},
				null
				}
			};
			ss.orderList = ss.orderList.FindAll(x =>x!=null&&!string.IsNullOrEmpty(x.orderId));
			string result = Test1.GetJson(ss);
			Console.WriteLine(result);
		}
	}

	public class Order
	{
		public List<OrderDetail> orderList { get; set; }
	}
	public class OrderDetail
	{
		public string orderId { get; set; }
		public int status { get; set; }
		public string creationTime { get; set; }
		public string overTime { get; set; }
		public string payTime { get; set; }
		public string expireTime { get; set; }
		public string rejectTime { get; set; }
		public string cancelTime { get; set; }
		public string departure { get; set; }
		public string arrival { get; set; }
		public string departureTime { get; set; }
		public string departureDate { get; set; }
		public string arrivalTime { get; set; }
		public string flightNo { get; set; }
		public float shouldPay { get; set; }
		public float realPay { get; set; }
		public int mailStatus { get; set; }
		public int mailType { get; set; }
		public string mailNo { get; set; }
	}

	public class Test1 : IDisposable
	{
		public void Dispose()
		{
			Console.WriteLine("已执行test的Dispose"); ;
		}

		public static string GetJson(object obj)
		{
			if (obj == null) return string.Empty;
			string result;
			var serializer = new DataContractJsonSerializer(obj.GetType());
			using (var ms = new MemoryStream())
			{
				serializer.WriteObject(ms, obj);
				result = Encoding.UTF8.GetString(ms.ToArray());
			}
			return result;
		}
	}
}
